package com.lxm.shownews.channel.fragment;

import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.PopupWindow;

import com.lxm.shownews.R;
import com.lxm.shownews.bean.ChannelInfo;
import com.lxm.shownews.channel.DrawerActivity;
import com.lxm.shownews.channel.adapter.ChannelPagerAdapter;
import com.lxm.shownews.channel.widget.ChannelPopup;
import com.lxm.shownews.commons.DBUtils;
import com.lxm.shownews.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * Created by lxm on 2016/1/28.
 */
public class AllChannelFragment extends Fragment implements View.OnClickListener {

    private TabLayout tabLayout;
    private ViewPager channelViewPager;
    private ChannelPagerAdapter channelAdapter;
    private List<BaseFragment> fragmentList;
    private List<String> titles = new ArrayList<>();
    private Button allBtn;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_allchannel, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
        channelViewPager = (ViewPager) view.findViewById(R.id.channel_viewPager);
        List<ChannelInfo> channelInfos = DBUtils.getDbInstance(this.getActivity()).selectSortChannelInfos();
        for (ChannelInfo channelInfo : channelInfos) {
            titles.add(channelInfo.getName());
            tabLayout.addTab(tabLayout.newTab());//.setText(channelInfo.getName())
        }
        fragmentList = new ArrayList<>();
        for (int i = 0; i < channelInfos.size(); i++) {
            fragmentList.add(new BaseFragment());
        }
        channelAdapter = new ChannelPagerAdapter(this.getFragmentManager(), fragmentList, titles);
        tabLayout.setTabsFromPagerAdapter(channelAdapter);
        channelViewPager.setAdapter(channelAdapter);
        tabLayout.setupWithViewPager(channelViewPager);
        allBtn = (Button) view.findViewById(R.id.all);
        allBtn.setOnClickListener(this);
    }


    //    private void initPop(View view) {
//        View inflate = LayoutInflater.from(this.getActivity())
//                .inflate(R.layout.channel_popup, null);
//        PopupWindow popupWindow = new PopupWindow(inflate,
//                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
//
//        popupWindow.setTouchable(true);
//        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#BFBFBF")));
//        popupWindow.showAsDropDown(view);
//    }
    private List<ChannelInfo> a() {
        return DBUtils.getDbInstance(this.getActivity()).selectSortChannelInfos();
    }

    @Override
    public void onClick(View v) {
        DrawerActivity mActivity = (DrawerActivity) this.getActivity();
//        initPop(mActivity.drawerToolbar);

        ChannelPopup channelPopup = new ChannelPopup(this.getActivity(), DBUtils.getDbInstance(this.getActivity()).selectSortChannelInfos());
        channelPopup.showPopup(mActivity.drawerToolbar);
    }
}
